<?php

namespace app\controllers;

use Yii;
use app\tools\Re;
use app\models\User;
use app\models\Good;
use app\models\GoodCate;
use app\models\OrderInfoGood;
use app\models\OrderInfoGoodEvaluate;
use app\models\UserCollect;
use app\models\UserHistory;
use app\models\ShoppingCart;

class GoodController extends BaseController
{
    //商品分类列表
    public function actionGoodCateList(){
      $good_cate_list = GoodCate::find()
        ->where(['status' => 1])
        ->select("id, name")
        ->orderBy("sort desc")
        ->all();
      return Re::data(['data' => $good_cate_list]);
    }

    //具体商品分类下所有商品列表
    public function actionGoodListByCate(){
      $page = Yii::$app->request->post('page', 0);
      $page_size = Yii::$app->request->post('page_size', 10);
      $cate_id = Yii::$app->request->post('cate_id', 0);
      $good_name = Yii::$app->request->post('good_name', '');
      $order_type = Yii::$app->request->post('order_type', 1);
      //1:按照设置的排序 2:销量正序 3:销量倒序 4:价格正序  5:价格倒序
      $good_query = Good::find()
        ->where(['cate_id' => $cate_id, 'status' => 1]);
      if ($good_name) {
        $good_query->andWhere(['like', 'name', $good_name]);
      }
      if ($order_type == 1) {
        $good_query->orderBy("sort desc");
      }
      if ($order_type == 2) {
        $good_query->orderBy("sale_count asc, sort desc");
      }
      if ($order_type == 3) {
        $good_query->orderBy("sale_count desc, sort desc");
      }
      if ($order_type == 4) {
        $good_query->orderBy("price asc, sort desc");
      }
      if ($order_type == 5) {
        $good_query->orderBy("price desc, sort desc");
      }
      $good_list = $good_query->limit($page_size)->offset($page_size*$page)->all();
      $good_data = [];
      if ($good_list) {
        foreach ($good_list as $v) {
          $good_data[] = [
            'good_id' => $v->id,
            'good_name' => $v->good->name,
            'good_img' => $v->good->img,
            'good_price' => $v->good->price,
          ];
        }
      }
      return Re::data(['data' => $good_data]);
    }

    //商品详情
    public function actionGoodDetail(){
      $good_id = Yii::$app->request->post('good_id', 0);
      $openid = Yii::$app->request->post('openid', '');
      $good_info = Good::find()->where(['id' => $good_id, 'status' => 1])->one();
      if (!$good_info) {
        return Re::data(null, '商品信息不存在', Re::ERROR);
      }
      $user_info = User::find()->where(['openid' => $openid, 'status' => 1])->one();
      if (!$user_info) {
        return Re::data(null, '用户信息读取错误', Re::ERROR);
      }
      UserHistory::saveRecord($good_id, $user_info->id);
      $evaluate_count = OrderInfoGoodEvaluate::find()->where(['good_id' => $good_id, 'status' => 1])->count();
      $good_data = [
        'good_cate_name' => $good_info->cate->name,
        'good_name' => $good_info->name,
        'good_img' => $good_info->img,
        'good_imgs' => $good_info->imgs,
        'good_price' => $good_info->price,
        'good_store' => $good_info->store,
        'sale_count' => $good_info->sale_count,
        'desc' => $good_info->desc,
        'detail_info' => $good_info->detail_info,
        'evaluate_count' => $evaluate_count,
        'is_collect' => UserCollect::find()->where(['user_id' => $user_info->id, 'good_id' => $good_id])->one()
      ];
      return Re::data(['data' => $good_data]);
    }

    //商品搜索
    public function actionSearch(){
      $keyword = Yii::$app->request->post('keyword', '');
      $page = Yii::$app->request->post('page', 0);
      if (empty($keyword)) {
        return Re::data(null, '未读取到搜索关键词', Re::ERROR);
      }
      $good_list = Good::find()
        ->where(['status' => 1])
        ->andWhere(['like', 'name', $keyword])
        ->orderBy('sort desc, created_at desc')
        ->limit(10)
        ->offset(10*$page)
        ->all();
      $good_data = [];
      if ($good_list) {
        foreach ($good_list as $v) {
          $good_data[] = [
            'good_id' => $v->id,
            'good_name' => $v->name,
            'good_img' => $v->img,
            'good_price' => $v->price,
          ];
        }
      }
      return Re::data(['data' => $good_data]);
    }

    //商品评价统计信息
	  public function actionGoodEvaluateInfo() {
      $good_id = Yii::$app->request->post('good_id', 0);
      $good_info = Good::find()->where(['id' => $good_id, 'status' => 1])->one();
      if (!$good_info) {
        return Re::data(null, '商品信息不存在', Re::ERROR);
      }
      $order_evaluate_hp = OrderInfoGoodEvaluate::find()->where(['good_id' => $good_id, 'status' => 1])->andWhere("star >= 3")->count();//好评
      $order_evaluate_zp = OrderInfoGoodEvaluate::find()->where(['good_id' => $good_id, 'status' => 1, 'star' => 2])->count();//中评
      $order_evaluate_cp = OrderInfoGoodEvaluate::find()->where(['good_id' => $good_id, 'status' => 1, 'star' => 1])->count();//差评
      $order_evaluate_img = OrderInfoGoodEvaluate::find()->where(['good_id' => $good_id, 'status' => 1])->andWhere("imgs <> ''")->count();//晒图
      $order_evaluate_video = OrderInfoGoodEvaluate::find()->where(['good_id' => $good_id, 'status' => 1])->andWhere("video <> ''")->count();//视频
      $data = [
        'order_evaluate_hp' => $order_evaluate_hp,
        'order_evaluate_zp' => $order_evaluate_zp,
        'order_evaluate_cp' => $order_evaluate_cp,
        'order_evaluate_img' => $order_evaluate_img,
        'order_evaluate_video' => $order_evaluate_video,
      ];
      return Re::data(['data' => $data]);
    }

    //商品评价列表
	  public function actionGoodEvaluateList() {
      $good_id = Yii::$app->request->post('good_id', 0);
      $type = Yii::$app->request->post('type', 0);
      $page = Yii::$app->request->post('page', 0);
      $page_size = Yii::$app->request->post('page_size', 10);
      $good_info = Good::find()->where(['id' => $good_id, 'status' => 1])->one();
      if (!$good_info) {
        return Re::data(null, '商品信息不存在', Re::ERROR);
      }
      $order_evaluate_query = OrderInfoGoodEvaluate::find()->where(['good_id' => $good_id, 'status' => 1]);
      if ($type == 1) {//好评
        $order_evaluate_query->andWhere("star >= 3");
      } else if ($type == 2) {//中评
        $order_evaluate_query->andWhere(['star' => 2]);
      } else if ($type == 3) {//差评
        $order_evaluate_query->andWhere(['star' => 1]);
      } else if ($type == 4) {//晒图
        $order_evaluate_query->andWhere("imgs <> ''");
      } else if ($type == 5) {//视频
        $order_evaluate_query->andWhere("video <> ''");
      }
      $order_evaluate_list = $order_evaluate_query->orderBy("created_at desc")->limit($page_size)->offset($page_size*$page)->all();
      $order_evaluate_data = [];
      if ($order_evaluate_list) {
        foreach ($order_evaluate_list as $v) {
          $order_evaluate_data[] = [
            'id' => $v->id,
            'star' => $v->star,
            'video' => $v->video,
            'imgs' => $v->imgs,
            'content' => $v->content,
            'user_phone' => $v->user->phone,
            'user_icon' => $v->user->icon,
            'created_time' => date('Y-m-d H:i:s', $v->created_at),
          ];
        }
      }
      return Re::data(['data' => $order_evaluate_data]);
    }
}
